﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using Commlibrary;

namespace StockAssistant
{
    public class StockMonitThread
    {
        ManualResetEvent m_eventMailStop;
        ManualResetEvent m_eventMailStopped;
        ArrayList m_arrStockInfo;
        StockHelp stockhelp;
        frmAssistant m_oFrmParent;

        public StockMonitThread(
            ManualResetEvent eventMailStop, 
            ManualResetEvent eventStopped,
            ArrayList arrStockInfo,
            frmAssistant oFrmParent)
		{
            this.m_eventMailStop = eventMailStop;
            this.m_eventMailStopped = eventStopped;
            this.m_arrStockInfo = arrStockInfo;
            stockhelp = new StockHelp();
            this.m_oFrmParent = oFrmParent;
		}

        public void Run()
        {
            while (true)
            {
                if (this.m_eventMailStop.WaitOne(0, true))
                {
                    this.m_eventMailStopped.Set();
                    return;
                }

                if (this.m_arrStockInfo != null && this.m_arrStockInfo.Count > 0)
                {
                    for (int iLoop = 0; iLoop < this.m_arrStockInfo.Count; iLoop++)
                    {
                        CStockInfo stockInfo = (CStockInfo)this.m_arrStockInfo[iLoop];
                        String sStockNo = stockInfo.sStockNo;
                        int iBuyNum = stockInfo.iStockBuyNum;
                        Decimal dBuyPrice = stockInfo.dStockBuyPrice;
                        Decimal dCurrPrice = 0;

                        string[] info = stockhelp.getStockInfoByCode(sStockNo);
                        String sCurrPrice = info[3];
                        if (sCurrPrice.Length > 0)
                        { 
                            dCurrPrice = Decimal.Parse(sCurrPrice);
                            Decimal dProfit = (dCurrPrice - dBuyPrice) * iBuyNum;
                            
                            if (dProfit > 0)
                            { 
                                // 印花
                                Decimal dStampDuty = dProfit * Decimal.Parse("0.001");
                                // 手续
                                Decimal dCommissionCharge = dProfit * Decimal.Parse("0.001");
                                if (dCommissionCharge < 5)
                                {
                                    dCommissionCharge = 5;
                                }
                                // 过户
                                Decimal dTransferFee = dProfit * Decimal.Parse("0.001");
                                if (dTransferFee < 1)
                                {
                                    dTransferFee = 1;
                                }

                                dProfit = dProfit - dStampDuty - dCommissionCharge - dTransferFee;
                            }

                            if (dProfit < 0)
                            {
                                dProfit = 0;
                            }

                            this.FindProfit(sStockNo, sCurrPrice, dProfit.ToString());
                        }
                    }
                }

                
                Thread.Sleep(30000);
            }
        }

        private void FindProfit(String sStockNo, String sCurrPrice, String sProfit)
        {
            try
            {
                if (this.m_eventMailStop.WaitOne(0, true))
                {
                    this.m_eventMailStopped.Set();
                    return;
                }
                this.m_oFrmParent.Invoke(this.m_oFrmParent.m_delegateFindProfit, new object[] { sStockNo, sCurrPrice, sProfit });
            }
            catch (System.Exception exp)
            {
                throw exp;
            }
        }
    }
}
